package org.szgb.console.sys.line.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.line.entity.SysLineLocationSystemEquipment;
import org.szgb.console.sys.line.mapper.SysLineLocationSystemEquipmentMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysLineLocationSystemEquipmentService {
    private final SysLineLocationSystemEquipmentMapper sysLineLocationSystemEquipmentMapper;

    @Autowired
    public SysLineLocationSystemEquipmentService(SysLineLocationSystemEquipmentMapper sysLineLocationSystemEquipmentMapper) {
        this.sysLineLocationSystemEquipmentMapper = sysLineLocationSystemEquipmentMapper;
    }

    @Transactional(readOnly = true)
    public SysLineLocationSystemEquipment get(String id) {
        return sysLineLocationSystemEquipmentMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysLineLocationSystemEquipment> findList(SysLineLocationSystemEquipment sysLineLocationSystemEquipment) {
        if (sysLineLocationSystemEquipment != null && sysLineLocationSystemEquipment.getPageDto() != null) {
            Page<SysLineLocationSystemEquipment> page = new Page<>(sysLineLocationSystemEquipment.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysLineLocationSystemEquipment.setPage(page);
        }
        return sysLineLocationSystemEquipmentMapper.findList(sysLineLocationSystemEquipment);
    }

    @Transactional(readOnly = true)
    public List<SysLineLocationSystemEquipment> getByLocationSystemIdArray(String[] ids) {
        return sysLineLocationSystemEquipmentMapper.getByLocationSystemIdArray(ids);
    }

    @Transactional(readOnly = true)
    public Integer getCount(SysLineLocationSystemEquipment sysLineLocationSystemEquipment) {
        return sysLineLocationSystemEquipmentMapper.getCount(sysLineLocationSystemEquipment);
    }

    @Transactional()
    public void save(SysLineLocationSystemEquipment sysLineLocationSystemEquipment) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysLineLocationSystemEquipment.getId() == null || sysLineLocationSystemEquipment.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysLineLocationSystemEquipment.setCreatedBy(sysLineLocationSystemEquipment.getUpdatedBy());
            // 添加UUID
            sysLineLocationSystemEquipment.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysLineLocationSystemEquipmentMapper.insert(sysLineLocationSystemEquipment);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysLineLocationSystemEquipmentMapper.update(sysLineLocationSystemEquipment);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysLineLocationSystemEquipmentMapper.delete(ids.split(","));
    }
}
